/**
 * @name 用户操作类
 * @author Yang
 * @date 2018-07-01
 * @version 1.0.1
 */
var Person= function () {

    var errors={
        'position':{
            success:false,
            message:"请上选择岗位"
        },
        'department':{
            success:false,
            message:"请选择部门"
        },
        'role':{
            success:false,
            message:"请选择权限"
        }
    }

    /**
     * 初始化表单的函数
     */
    var formInitHandler= function () {
        console.log("this is ok");
        $('.i-checks').iCheck({
            checkboxClass: "icheckbox_square-green",
            radioClass: "iradio_square-green"})
    };

    var selectHandler=function () {
        $('.ibox').on("click",'button.position',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe=layers.find('iframe');
                    var radio= iframe.contents().find('input[type=radio]:checked');
                    if (radio.length==0){
                        parent.layer.close(index);
                        Common.alert(errors.position);
                        return;
                    }
                    var id= radio.data('id');
                    var name= radio.data('name');
                    console.log(id);
                    console.log(name);
                    $("#positionId").val(id);
                    $('#positionName').val(name);
                    parent.layer.close(index);
                },
                content:'../position/index.html?check=ok'
            })
        });

        $('.ibox').on("click",'button.department',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe= layers.find('iframe');
                    var radio= iframe.contents().find('input[type=radio]:checked');
                    if (radio.length==0){
                        parent.layer.close(index);
                        Common.alert(errors.department);
                        return;
                    }
                    var name= radio.data('name');
                    var id=radio.data('id');
                    console.log(name);
                    console.log(id);
                    $('#departmentId').val(id);
                    $('#departmentName').val(name);
                    parent.layer.close(index);
                },
                content:'../department/index.html?check=ok'
            })
        });

        $('.ibox').on("click",'button.role',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function (index,layers) {
                    var iframe= layers.find('iframe');
                    var radio= iframe.contents().find('input[type=radio]:checked');
                    if (radio.length==0){
                        parent.layer.close(index);
                        Common.alert(errors.role);
                        return;
                    }
                    var name= radio.data("name");
                    var id= radio.data("id");
                    console.log(name);
                    console.log(id);
                    $("#roleId").val(id);
                    $('#roleName').val(name);
                    parent.layer.close(index);
                },
                content:"../role/index.html?check=ok"
            })
        })
    };

    /**
     * 上传文件
     *
     */
    var uploadHandler= function () {
        Common.upload("portraitFile",1,"portrait");
        Common.upload("pictureFile",2,"picture");
        $(".date").datepicker({
            todayBtn: "linked",
            keyboardNavigation: !1,
            forceParse: !1,
            calendarWeeks: !0,
            autoclose: !0
        })
    }

    var validateHandler=function () {
        var form= $('form');
        Common.validate();
        form.validate({
            rules:{
                username:{
                    required:true,
                    minlength:5,
                    maxlength:32,
                    remote:{
                        type:"get",
                        url:"validateUsername"
                    }
                },
                phoneNumber:{
                    required:true,
                    phone:true,
                },
                email:{
                    email:true
                },
                realName:{
                    required:true
                }
            },
            messages:{
                username:{
                    required:"必填",
                    minlength:"至少5个字段",
                    maxlength:"最多32个字符",
                    remote:"用户名已经存在"
                },
                phoneNumber:{
                    required:"必填",
                    phone:"手机号码的格式不正确"
                },
                email:{
                    email:"电子邮件格式不正确"
                },
                realName:{
                    required:"必填"
                }
            },
            submitHandler:function (form) {
                var departmentId= $("#departmentId").val();
                if (departmentId==null){
                    Common.alert(errors.department);
                    return;
                }
                var roleId= $("#roleId").val();
                if (roleId==null){
                    Common.alert(errors.role);
                    return;
                }
                var positionId= $("#positionId").val();
                if (positionId==null){
                    Common.alert(errors.position);
                    return;
                }
                var jobTime= $("#jobTime").val();
                if (jobTime!=null && jobTime!=""){
                    jobTime= new Date(jobTime).getTime();
                }
                var params={};
                params.username= $('#username').val();
                params.phone= $('#phoneNumber').val();
                params.email= $("#email").val();
                params.realName= $("#realName").val();
                params.address= $("#address").val();
                params.telphone= $("#telPhone").val();
                params.roleId= roleId;
                params.departmentId= departmentId;
                params.positionId= positionId;
                params.portrait= $("#portrait").val();
                params.picture= $("#picture").val();
                params.education= $("#education").val();
                params.sex= $('input[name=gender]:checked').val();
                if (jobTime!=null){
                    params.jobTime= jobTime;
                }
                params= JSON.stringify(params);
                $.ajax({
                    type:"post",
                    url:"submit",
                    data:{"params":params},
                    dataType:"json",
                    success:function (data) {
                        Common.alert(data);
                        if (data.success){
                            setTimeout(function () {
                                location.href="index.html";
                            },1500)
                        }
                    }
                })
            }
        })
    }

    return {
        //添加的事件
        add:function () {
            formInitHandler();
            selectHandler();
            validateHandler();
            uploadHandler();
        }
    }
}();